package com.xnyzc.lhy.resource.controller.worksheet;


import com.xnyzc.lhy.common.component.heli.HeLiUrlContant;
import com.xnyzc.lhy.common.entity.DiDiRv;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.entity.WorksheetRv;
import com.xnyzc.lhy.common.exception.WorksheetException;
import com.xnyzc.lhy.common.penum.EDiDiErrorCode;
import com.xnyzc.lhy.common.penum.worksheet.WorksheetErrorCode;
import com.xnyzc.lhy.common.util.StringUtil;
import com.xnyzc.lhy.common.util.sign.AttestationTool;
import com.xnyzc.lhy.order.entity.param.synchronization.ChangePriceParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.DetectionToken;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetCancelOrderParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetCloseOrderParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetFreeChargeParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetGetBillRefundParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetGetOrderPersonalDetailParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetGetOrderPersonalStatusParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetUpdateWorksheetParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetetDriverPenaltyDetailParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetetGetDriverDetailParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.WorksheetgetOrderPersonalListParam;
import com.xnyzc.lhy.resource.entity.worksheet.param.heli.FromHeliWorksheetParam;
import com.xnyzc.lhy.resource.feign.worksheet.IOaAuthService;
import com.xnyzc.lhy.resource.service.worksheet.WorksheetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 外部系统操作工单接口
 * </p>
 *
 * @author haoyfa
 * @since 2019-11-04
 */
@RestController
@Api(value = "外部系统操作工单接口", tags = "外部系统操作工单接口")
@RequestMapping("/worksheet")
public class WorksheetController {

    @Autowired
    WorksheetService worksheetService;

    @Autowired
    private IOaAuthService iOaAuthService;

    /**
     * 取消订单
     *
     * @param worksheetCancelOrderParam
     * @return
     */
    @ApiOperation(value = "取消订单", notes = "取消订单")
    @PostMapping(value = "/order/cancelOrder")
    public WorksheetRv cancelOrder(@ApiParam(name =
            "worksheetCancelOrderParam", value = "取消订单") @RequestBody WorksheetCancelOrderParam worksheetCancelOrderParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetCancelOrderParam, worksheetCancelOrderParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(worksheetCancelOrderParam.getUserToken())
                || !this.getDetectionToken(worksheetCancelOrderParam.getUserToken())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(worksheetCancelOrderParam.getOrderId())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            // 取消订单
            worksheetService.cancelOrder(worksheetCancelOrderParam);
            return WorksheetRv.wrapSuccess(new Object());
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.notOrderStatus);
        }
    }

    /**
     * 关闭订单
     *
     * @param worksheetCloseOrderParam
     * @return
     */
    @ApiOperation(value = "关闭订单", notes = "关闭订单")
    @PostMapping(value = "/order/getCloseOrder")
    public DiDiRv getCloseOrder(@ApiParam(name =
            "worksheetCancelOrderParam", value = "关闭订单") @RequestBody WorksheetCloseOrderParam worksheetCloseOrderParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetCloseOrderParam, worksheetCloseOrderParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(worksheetCloseOrderParam.getUser_token())
                || !this.getDetectionToken(worksheetCloseOrderParam.getUser_token())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(worksheetCloseOrderParam.getOrder_id())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            // 关闭订单
            return worksheetService.getCloseOrder(worksheetCloseOrderParam);
        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 订单改价接口
     *
     * @param priceParam
     * @return
     */
    @ApiOperation(value = "订单改价接口", notes = "订单改价接口")
    @PostMapping(value = "/order/getChangePrice")
    public DiDiRv getChangePrice(@ApiParam(name =
            "worksheetGetChangePriceParam", value = "订单改价接口") @RequestBody ChangePriceParam priceParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(priceParam, priceParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(priceParam.getUser_token())
                || !this.getDetectionToken(priceParam.getUser_token())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(priceParam.getOrder_id())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            // 订单改价
            return worksheetService.getChangePrice(priceParam);
        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 退款接口
     *
     * @param billRefundParam
     * @return
     */
    @ApiOperation(value = "退款接口", notes = "退款接口")
    @PostMapping(value = "/order/getBillRefund")
    public DiDiRv getBillRefund(@ApiParam(name =
            "worksheetGetBillRefundParam", value = "退款接口") @RequestBody WorksheetGetBillRefundParam billRefundParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(billRefundParam, billRefundParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.failureOfVerification);
        }

//         用户token验证
        if (StringUtils.isBlank(billRefundParam.getUser_token())
                || !this.getDetectionToken(billRefundParam.getUser_token())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(billRefundParam.getOrder_id())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            // 退款接口
            return worksheetService.getBillRefund(billRefundParam);
        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 免单接口
     *
     * @param worksheetFreeChargeParam
     * @return
     */
    @ApiOperation(value = "免单接口", notes = "免单接口")
    @PostMapping(value = "/order/getFreeCharge")
    public DiDiRv getFreeCharge(@ApiParam(name =
            "worksheetFreeChargeParam", value = "免单接口") @RequestBody WorksheetFreeChargeParam worksheetFreeChargeParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetFreeChargeParam, worksheetFreeChargeParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(worksheetFreeChargeParam.getUserToken())
                || !this.getDetectionToken(worksheetFreeChargeParam.getUserToken())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(worksheetFreeChargeParam.getOrderId())) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.parameterError);
        }
        try {
            // 免单接口

            return worksheetService.getFreeCharge(worksheetFreeChargeParam);
        } catch (WorksheetException ex) {
            return DiDiRv.wrapErrorCode(EDiDiErrorCode.notOrderStatus);
        }
    }

    /**
     * 合力工单状态同步第三方
     *
     * @param worksheetUpdateWorksheetParam
     * @return
     */
    @ApiOperation(value = "工单状态同步第三方（滴滴）接口", notes = "工单状态同步第三方（滴滴）接口")
    @PostMapping(value = "/updateWorksheet")
    public WorksheetRv updateWorksheet(@ApiParam(name =
            "worksheetUpdateWorksheetParam", value = "工单状态同步第三方（滴滴）接口") @RequestBody WorksheetUpdateWorksheetParam worksheetUpdateWorksheetParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetUpdateWorksheetParam, worksheetUpdateWorksheetParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(worksheetUpdateWorksheetParam.getUserToken())
                || !this.getDetectionToken(worksheetUpdateWorksheetParam.getUserToken())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(worksheetUpdateWorksheetParam.getWorksheetId())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            // 工单状态同步第三方
            worksheetService.updateWorksheet(worksheetUpdateWorksheetParam);
            return WorksheetRv.wrapSuccess();
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.updateWorksheetError);
        }
    }

    /**
     * 查询订单详情接口
     *
     * @param worksheetGetOrderPersonalDetailParam
     * @return
     */
    @ApiOperation(value = "查询订单详情接口", notes = "查询订单详情接口")
    @PostMapping(value = "/order/getOrderPersonalDetail")
    public WorksheetRv getOrderPersonalDetail(@ApiParam(name =
            "worksheetGetOrderPersonalDetailParam", value = "免单接口") @RequestBody WorksheetGetOrderPersonalDetailParam worksheetGetOrderPersonalDetailParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetGetOrderPersonalDetailParam, worksheetGetOrderPersonalDetailParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return WorksheetRv.wrapErrorCodeArray(WorksheetErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(worksheetGetOrderPersonalDetailParam.getUserToken())
                || !this.getDetectionToken(worksheetGetOrderPersonalDetailParam.getUserToken())) {
            return WorksheetRv.wrapErrorCodeArray(WorksheetErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(worksheetGetOrderPersonalDetailParam.getOrderId())) {
            return WorksheetRv.wrapErrorCodeArray(WorksheetErrorCode.parameterError);
        }
        try {
            // 获取订单信息
            return worksheetService.getOrderPersonalDetail(worksheetGetOrderPersonalDetailParam);
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCodeArray(WorksheetErrorCode.notOrderStatus);
        }
    }


    /**
     * 查询订单状态接口
     *
     * @param worksheetGetOrderPersonalStatusParam
     * @return
     */
    @ApiOperation(value = "查询订单状态接口", notes = "查询订单状态接口")
    @PostMapping(value = "/order/getOrderPersonalStatus")
    public WorksheetRv getOrderPersonalStatus(@ApiParam(name =
            "worksheetGetOrderPersonalStatusParam", value = "查询订单状态接口") @RequestBody WorksheetGetOrderPersonalStatusParam worksheetGetOrderPersonalStatusParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetGetOrderPersonalStatusParam, worksheetGetOrderPersonalStatusParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.failureOfVerification);
        }

        if (StringUtils.isBlank(worksheetGetOrderPersonalStatusParam.getOrderId())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            // 获取订单状态
            return worksheetService.getOrderPersonalStatus(worksheetGetOrderPersonalStatusParam);
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.notOrderStatus);
        }
    }

    /**
     * 查询订单列表接口
     *
     * @param worksheetgetOrderPersonalListParam
     * @return
     */
    @ApiOperation(value = "查询订单详情接口", notes = "查询订单详情接口")
    @PostMapping(value = "/order/getOrderPersonalList")
    public WorksheetRv getOrderPersonalList(@ApiParam(name =
            "worksheetgetOrderPersonalListParam", value = "查询订单详情接口") @RequestBody WorksheetgetOrderPersonalListParam worksheetgetOrderPersonalListParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetgetOrderPersonalListParam, worksheetgetOrderPersonalListParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(worksheetgetOrderPersonalListParam.getUserToken())
                || !this.getDetectionToken(worksheetgetOrderPersonalListParam.getUserToken())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(worksheetgetOrderPersonalListParam.getCusPhone())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            // 获取订单列表
            return worksheetService.getOrderPersonalList(worksheetgetOrderPersonalListParam);
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.notOrderStatus);
        }
    }

    /**
     * 客服标记司机违规
     *
     * @param worksheetetDriverPenaltyDetailParam
     * @return
     */
    @ApiOperation(value = "客服标记司机违规", notes = "客服标记司机违规")
    @PostMapping(value = "/driver/setDriverPenaltyDetail")
    public WorksheetRv setDriverPenaltyDetail(@ApiParam(name =
            "worksheetetDriverPenaltyDetailParam", value = "客服标记司机违规") @RequestBody WorksheetetDriverPenaltyDetailParam worksheetetDriverPenaltyDetailParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetetDriverPenaltyDetailParam, worksheetetDriverPenaltyDetailParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(worksheetetDriverPenaltyDetailParam.getUserToken())
                || !this.getDetectionToken(worksheetetDriverPenaltyDetailParam.getUserToken())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(worksheetetDriverPenaltyDetailParam.getDriverId())
                || StringUtils.isBlank(worksheetetDriverPenaltyDetailParam.getOrderId())
                || StringUtils.isBlank(worksheetetDriverPenaltyDetailParam.getDeregulationType())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            // 客服标记司机违规
            return worksheetService.setDriverPenaltyDetail(worksheetetDriverPenaltyDetailParam);
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.notOrderStatus);
        }
    }

    /**
     * 司机信息接口
     *
     * @param worksheetetGetDriverDetailParam
     * @return
     */
    @ApiOperation(value = "司机信息接口", notes = "司机信息接口")
    @PostMapping(value = "/driver/getDriverDetail")
    public WorksheetRv getDriverDetail(@ApiParam(name =
            "worksheetetGetDriverDetailParam", value = "免单接口") @RequestBody WorksheetetGetDriverDetailParam worksheetetGetDriverDetailParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(worksheetetGetDriverDetailParam, worksheetetGetDriverDetailParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(worksheetetGetDriverDetailParam.getUserToken())
                || !this.getDetectionToken(worksheetetGetDriverDetailParam.getUserToken())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(worksheetetGetDriverDetailParam.getDriverPhone())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }
        try {
            // 司机信息接口
            return worksheetService.getDriverDetail(worksheetetGetDriverDetailParam);
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.notOrderStatus);
        }
    }

    /**
     * 创建工单
     *
     * @param fromHeliWorksheetParam
     * @return
     */
    @ApiOperation(value = "创建工单", notes = "创建工单")
    @PostMapping(value = "/createWorksheet")
    public WorksheetRv createWorksheet(@ApiParam(name =
            "fromHeliWorksheetParam", value = "创建工单") @RequestBody FromHeliWorksheetParam fromHeliWorksheetParam) {
        //验签处理
        if (!AttestationTool.getRequestSign(fromHeliWorksheetParam, fromHeliWorksheetParam.getSign(), HeLiUrlContant.heLiSignReg)) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.failureOfVerification);
        }

        // 用户token验证
        if (StringUtils.isBlank(fromHeliWorksheetParam.getUserToken())
                || !this.getDetectionToken(fromHeliWorksheetParam.getUserToken())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.userTookenVerification);
        }

        if (StringUtils.isBlank(fromHeliWorksheetParam.getWorksheet_id())) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.parameterError);
        }

        try {
            // 创建工单接口
            return worksheetService.createWorksheet(fromHeliWorksheetParam);
        } catch (WorksheetException ex) {
            return WorksheetRv.wrapErrorCode(WorksheetErrorCode.createWorksheetError);
        }
    }

    /**
     * 判断访问权限验证
     */
    private Boolean getDetectionToken(String userToken) {
        DetectionToken detectionToken = new DetectionToken();
        detectionToken.setToken(userToken);

        Rv result = iOaAuthService.getDetectionToken(detectionToken);

        if (result.getData() != null
                && !"".equals(StringUtil.isNullReturnEmpty(result.getData().toString()))) {
            return true;
        } else {
            return false;
        }
    }

}
